var express = require('express');
var router = express.Router()
var { graphql } = require('graphql')
var schema = require('../schema/index')

/* GET home page. */
router.get('/', async function(req, res, next) {
  let query = `
    {
      movie {
        id,
        title,
        rating
      },
      position {
        positionId,
        positionName
      }
    }
  `

  let result = await graphql(schema, query)

  res.json(result)
})

router.post('/', async (req, res, next) => {
  let {title, genres, rating, theater} = req.body
  let mutation = `
    mutation {
      insert(
        title: "${title}",
        genres: "${genres}",
        rating: ${rating},
        theater: ${theater}
      ) {
        ret,
        data
      }
    }
  `

  let result = await graphql(schema, mutation)

  res.json(result)
})

module.exports = router;

// {
//   patch(
//     id: 14,
//     title: "悬崖之下"
//   ) {
//     ret,
//     data
//   }
// }

// {
//   put(
//     id: 14,
//     title: "悬崖之上"
//   ) {
//     ret,
//     data
//   }
// }